from flask_restful import Resource, Api
from flask import Blueprint
from flask import jsonify, request, session
from datetime import datetime
from flask_login import login_required
import json
from Plugins.helper import des_encrypt, check_permission


class BaseView(Resource):

    def result(self, code=None, data=None):
        result_data = self.ResultCode[code].copy()

        if data is not None:
            result_data['data'] = data

        enc_data = des_encrypt(json.dumps(result_data, ensure_ascii=False))

        return jsonify(enc_data)

    __result_array = []

    def to_array(self, data_list):
        self.__result_array.clear()
        if not isinstance(data_list, list):
            return self.__result_array
        for obj in data_list:
            obj_dict = {}
            for (key, val) in obj.__dict__.items():
                if key == '_sa_instance_state':
                    continue
                elif isinstance(val, list):
                    continue
                elif isinstance(val, datetime):
                    obj_dict[key] = val.strftime('%Y-%m-%d %H:%M:%S')
                else:
                    obj_dict[key] = val
            self.__result_array.append(obj_dict)
        return self.__result_array

    ResultCode = {
        # 登录
        '0x00001': {'code': '0x00001', 'msg': '无此帐号'},
        '0x00002': {'code': '0x00002', 'msg': '密码错误'},
        '0x00003': {'code': '0x00003', 'msg': '验证失败'},
        '0x00004': {'code': '0x00004', 'msg': '登录异常'},
        '0x00005': {'code': '0x00005', 'msg': '登录成功'},
        '0x00006': {'code': '0x00006', 'msg': '帐号禁用'},
        '0x00007': {'code': '0x00007', 'msg': '退出登录'},
        '0x00008': {'code': '0x00008', 'msg': '请先登录'},
        '0x00009': {'code': '0x00009', 'msg': '已经登录'},

        # 数据请求
        '0x00010': {'code': '0x00010', 'msg': '请求成功'},
        '0x00011': {'code': '0x00011', 'msg': '请求失败'},

        # 增加数据
        '0x00012': {'code': '0x00012', 'msg': '增加成功'},
        '0x00013': {'code': '0x00013', 'msg': '增加失败'},

        # 删除数据
        '0x00014': {'code': '0x00014', 'msg': '删除成功'},
        '0x00015': {'code': '0x00015', 'msg': '删除失败'},

        # 修改数据
        '0x00016': {'code': '0x00016', 'msg': '修改成功'},
        '0x00017': {'code': '0x00017', 'msg': '修改失败'},

        # 操作信息
        '0x00018': {'code': '0x00018', 'msg': '操作成功'},
        '0x00019': {'code': '0x00019', 'msg': '操作失败'},

        # CSRF错误
        '0x00020': {'code': '0x00020', 'msg': '检查CSRF'},

        # 权限验证
        '0x00021': {'code': '0x00021', 'msg': '无此权限'},

        # 保存数据
        '0x00022': {'code': '0x00022', 'msg': '保存成功'},
        '0x00023': {'code': '0x00023', 'msg': '保存失败'},
    }

# from Common.BaseView import BaseView, Blueprint, Api, request
#
#
# module_bp = Blueprint('module', __name__, url_prefix='/api')
# api = Api(module_bp)
#
#
# class ModuleView(BaseView):
#     def get(self):
#         try:
#             pass
#         except Exception as error:
#             return self.result('0x00011', data='%s' % error)
#
#     def post(self):
#         try:
#             pass
#         except Exception as error:
#             return self.result('0x00013', data='%s' % error)
#
#     def put(self):
#         try:
#             pass
#         except Exception as error:
#             return self.result('0x00017', data='%s' % error)
#
#     def delete(self):
#         try:
#             pass
#         except Exception as error:
#             return self.result('0x00015', data='%s' % error)
#
#
# api.add_resource(ModuleView, '/module')
